﻿using Portal.Logic.Providers;
using Portal.Model;
using Portal.Presentation.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Portal.Presentation.Providers
{
    /// <summary>
    /// Class <see cref="SecurityProvider"/> define security provider
    /// </summary>
    public class SecurityProvider : ISecurityProvider
    {
        #region Fields
        private readonly ISessionService _session;
        private readonly IUserProvider _userProvider;
        #endregion

        #region .ctor
        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityProvider" /> class.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="userProvider">The user provider.</param>
        /// <param name="logonProvider">The logon provider.</param>
        /// <exception cref="System.ArgumentNullException">session</exception>
        public SecurityProvider(IUserProvider userProvider, ISessionService session)
        {
            if (session == null)
            {
                throw new ArgumentNullException("userInfoProvider");
            }
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }
            _session = session;
            _userProvider = userProvider;
        }
        #endregion

        #region Methods

        /// <summary>
        /// Gets current user
        /// </summary>
        /// <returns></returns>
        public IUser GetUser(string logon)
        {
            var user = _session.GetCurrentUser();
            if (user == null)
            {
                user = _userProvider.GetUser(logon);
                _session.SetCurrentUser(user);
            }
            return user;
        }
        #endregion
    }
}